package com.fbt.transfer.app.driver.activity;

import android.content.Intent;
import com.fbt.transfer.app.driver.model.AccessToken;
import com.fbt.transfer.app.driver.http.AbstractCallback;
import com.fbt.transfer.app.driver.util.Constants;
import com.fbt.transfer.app.driver.util.DialogUtils;
import com.fbt.transfer.app.driver.util.UserManager;
import com.fbt.transfer.app.driver.databinding.ActivityLoginBinding;

import com.fbt.transfer.app.driver.R;

public class LoginActivity extends BaseActivity {

  private long exitTime = 0;
  private int look = 0;
  private int box = 0;

  private ActivityLoginBinding activityLoginBinding;

  public LoginActivity () {
    this.activityLoginBinding = null;
  }

  @Override
  protected void initView () {
    activityLoginBinding = ActivityLoginBinding.inflate(getLayoutInflater());
    setContentView(activityLoginBinding.getRoot());
  }

  @Override
  protected void initListener () {
    activityLoginBinding.nameEt.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus) {
            activityLoginBinding.nameEt.setHint(Constants.EMPTY_STRING);
            activityLoginBinding.nameEt.setBackgroundResource(android.R.drawable.editbox_background_normal);
        } else {
            if (activityLoginBinding.nameEt.getText().toString().trim().isEmpty()) {
                activityLoginBinding.nameEt.setHint("请输入用户名");
                activityLoginBinding.nameEt.setBackgroundResource(R.drawable.red_border_animation);
            }
        }
    });
    // 登录按钮点击事件
    activityLoginBinding.passwordEt.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus) {
            activityLoginBinding.passwordEt.setHint(Constants.EMPTY_STRING);
            activityLoginBinding.passwordEt.setBackgroundResource(android.R.drawable.editbox_background_normal);
        } else {
            if (activityLoginBinding.passwordEt.getText().toString().trim().isEmpty()) {
                activityLoginBinding.passwordEt.setHint("请输入密码");
                activityLoginBinding.passwordEt.setBackgroundResource(R.drawable.red_border_animation);
            }
        }
    });
    activityLoginBinding.loginBtn.setOnClickListener(v -> {
      String username = activityLoginBinding.nameEt.getText().toString().trim();
      String password = activityLoginBinding.passwordEt.getText().toString().trim();
      if (username.isEmpty() || password.isEmpty()) {
        DialogUtils.showInfoToast(this, "请输入用户名和密码");
        return;
      }
      // 调用登录接口
      apiInterface.login(username, password).enqueue(new AbstractCallback<AccessToken>(this, true) {
        @Override
        protected void onSuccess (AccessToken token) {
          AccessToken.MemberVo memberVo = token.getMember();
          if (Boolean.TRUE.equals(memberVo.getIsCheTou())) {
            DialogUtils.showTip(LoginActivity.this, "不支持车队长登陆,请到调度端登陆", false);
            return;
          }
          UserManager.getInstance(LoginActivity.this).saveUserInfo(
              token.getToken(), memberVo.getMemberId(), memberVo.getLoginName());
          // 跳转到主界面
          Intent intent = new Intent(LoginActivity.this, MainActivity.class);
          startActivity(intent);
          finish();
        }
      });
    });
  }

}
